﻿// <copyright file="StorageBlockInfo.cs" company="Sinclair Community College">
// Copyright 2010 Sinclair Community College
// </copyright>
// <originalAuthor>Patrick S. Seymour (patrick.seymour@gmail.com)</originalAuthor>

namespace SinclairCC.AppV
{
    using System;

    public struct StorageBlockInfo
    {
        /// <summary>
        /// The start index of a contiguous run of containers.
        /// </summary>
        private uint allocationIndex;

        /// <summary>
        /// Indicates the number of storage containers referenced by the allocation index.
        /// </summary>
        private uint blockCount;

        /// <summary>
        /// Initializes a new instance of the StorageBlockInfo class.
        /// </summary>
        public StorageBlockInfo(uint allocationIndex, uint blockCount)
        {
            this.allocationIndex = allocationIndex;
            this.blockCount = blockCount;
        }

        /// <summary>
        /// Gets or sets the start index of a contiguous run of containers.
        /// </summary>
        public uint AllocationIndex
        {
            get { return this.allocationIndex; }
            set { this.allocationIndex = value; }
        }

        /// <summary>
        /// Gets or sets the number of CSR Containers referenced by the allocation index.
        /// </summary>
        public uint BlockCount
        {
            get { return this.blockCount; }
            set { this.blockCount = value; }
        }

        public long GetOffset(uint containerSize, uint storageBlockSize)
        {
            // The number of well-known containers at the beginning of the file.
            const long WellKnownContainerCount = 16;

            if ((this.blockCount == 0) && (this.allocationIndex == 0))
            {
                return -1;
            }
            else
            {
                //return (WellKnownContainerCount * containerSize) + (this.allocationIndex * storageBlockSize);
                return WellKnownContainerCount * containerSize + this.allocationIndex * storageBlockSize;
            }
        }

        public override string ToString()
        {
            return string.Format("alloc index: {0}, block count: {1}", this.allocationIndex, this.blockCount);
        }

    }
}
